<?php
$this->breadcrumbs = array();
if(isset($goods['cate']) && !empty($goods['cate'])){
	echo '<div class="breadcrumbs"> 当前商品分类：';
	echo GoodsCateModel::getCateFamilys($goods['cate'], ' > ', ' | ');
	echo '</div>';
}
// $this->breadcrumbs[] = $goods['name'];

$price = isset($goods['price']) ? $goods['price'] : 0;
if(isset($goods['specs']) && count($goods['specs'])>0){
	$spec_data = array(); 	//规格与价格/库存的映射
	$min_price = 0;			//最低价格
	$max_price = 0;			//最高价格
	$specs12 = array();	
	$spec1s = array();
	$spec2s = array();
	foreach ($goods['specs'] as $goods_spec_id => $item){
		$spec1 = $item['spec1']; //必需存在
		$spec1s[] = $spec1;
		if(isset($item['spec2'])){
			$spec2 = $item['spec2'];
			$spec2s[] = $spec2;
			$specs12[$spec1][] = $spec2;
			$specs12[$spec2][] = $spec1;
			$spec1 .= '_' . $spec2;
		}else{
			$specs12[$spec1] = null;
		}
		if(isset($goods_list[$goods_spec_id])){
			$g = $goods_list[$goods_spec_id];
			$spec_data[$spec1] = array(
				'spec_id' => $goods_spec_id,
				'price'		=> number_format($g->price, 2),
				'stock'		=> $g->stock,
			);
			$min_price = min($min_price, $g->price);
			$max_price = max($max_price, $g->price);
		}
		if($min_price != $max_price){
			$price = $min_price . ' - ' . $max_price;
		}
	}
	$spec1s = array_unique($spec1s);
	$spec2s = array_unique($spec2s);
}
?>

<div id="goods_main_info">
	<?php 
	$this->widget('application.widget.ad.AdWidget', array(
		'code' 	=> 'goods_top_1',
		'thumb' => null,
	));
	?>
	<div id="goods_info_left">
	<?php 
		//商品图片展示
		$this->widget('application.widget.goods.GoodsImageViewWidget', array(
			'goods' => $goods
		));
		//分享链接
		$this->widget('application.widget.JiaThisWidget', array(
		// 	'type' => 32
		));
		echo CHtml::link('加入收藏', 'javascript:void(0)', array('class'=>'mark_link', 'title'=>'加入收藏可以关注本商品的价格及优惠促销活动'));
	?>
	</div>
	<div id="goods_info">
		<?php echo CHtml::tag('h1', array(), $goods['name']);?>
		<label>商品编号：</label><?php echo isset($goods['code'])?$goods['code']:'--'?><br>
		<label>　商城价：</label>￥<span id="price"><?php echo $price?></span> 
		<?php if(isset($goods['market_price']) && !empty($goods['market_price'])){?>
		<label>市场价：</label><del>￥<?php echo number_format($goods['market_price'], 2)?></del>
		<?php }?><br>
		
		<label>客户评分：</label> 已有 5人评论，100%推荐<br>
		<label>收藏人气：</label> 102<br>
		<label>运费：</label> 平邮 <?php echo number_format($goods['post'], 2)?>　快递 <?php echo number_format($goods['express'], 2)?> 　EMS <?php echo number_format($goods['ems'], 2)?><br>
		<div class="buy_info">
		<?php 
		if(isset($goods['specs']) && count($goods['specs'])>0){
			if(!empty($spec1s)){
				echo '<dl class="spec1" spec="' . $goods['spec1'] . '">';
				echo CHtml::tag('dt', array(), $goods['spec1'] . '：');
				foreach ($spec1s as $spec_name){
					echo CHtml::tag('dd', array(), $spec_name . CHtml::tag('span', array()));
				}
				echo '</dl>';
			}
			if(!empty($spec2s)){
				echo '<dl class="spec2" spec="' . $goods['spec2'] . '">';
				echo CHtml::tag('dt', array(), $goods['spec2'] . '：');
				foreach ($spec2s as $spec_name){
					echo CHtml::tag('dd', array(), $spec_name . CHtml::tag('span', array()));
				}
				echo '</dl>';
			}
		}
		?>
			<label>数量：</label>
			<?php 
			$this->widget('application.widget.NumberInputWidget', array(
				'inputOptions' 		=> array('class'=>'buy_count'),
				'name'				=> 'count',
				'value'				=> isset($goods['min']) && !empty($goods['min'])?$goods['min']:1,
				'min'				=> isset($goods['min']) && !empty($goods['min'])?$goods['min']:1,
				'max'				=> isset($goods['max'])?$goods['max']:null,
				'error_callback'	=> 'count_error',
				'success_callback'	=> 'count_success',
			));
			echo CHtml::tag('span', array('class'=>'unit'), $goods['unit']);
			?><span id="stock"></span><br>
			<?php if(isset($goods['specs']) && count($goods['specs'])>0){?>
			<div id="selected_spec" class="red"><label>请选择：</label><span><?php echo '“' . $goods['spec1'] . '”' . (isset($goods['spec2']) && !empty($goods['spec2'])?'、“' . $goods['spec2'] . '”':'')?></span></div>
			<?php }?>
			<div class="options">
				<input type="button" class="button" value="马上购买" id="buy_btn"/>
				<input type="button" class="button" value="加入购物车" id="add_car_btn"/>
			</div>
			<input type="hidden" name="goods_spec_id" value="<?php echo $goods['_id'];?>"/>
		</div>
	</div>
</div>
<div id="refer_content">
	<?php 
	$this->widget('application.widget.goods_cate.GoodsCateListWidget', array(
		'count' => 5
	));
	$this->widget('application.widget.goods.GoodsListWidget', array(
		'count' => 5
	));
	echo CHtml::image(CommonHelper::get_thumb('/images/qr.png', '184x184'), '扫描二维码', array('class'=>'qr'));
	$this->widget('application.widget.ad.AdWidget', array(
		'thumb' => '186x186',
		'code' => 'goods_detail_left_1'
	));
	?>
</div>
<div id="goods_detail">
	<h3>商品描述</h3>
	<?php echo isset($goods['desc'])?$goods['desc']:''?>
</div>
<script>
	<?php 
		if(isset($specs12)){
			echo 'var SPECS12 = ' . json_encode($specs12) . ';';
		}
		if(isset($spec_data)){
			echo 'var SPEC_DATA = ' . json_encode($spec_data) . ';';
		}
	?>
</script>